﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_AddType : ATPages
{
    ZSql zsql = new ZSql();
    Function f = new Function();
    private void initType1()
    {
       
        ZSql Dep = new ZSql();
        Dep.Open("select * from Department ");
        if (Dep.m_table.Rows.Count > 0)
        {
            ddrDepartment.DataSource = Dep.m_table;
            ddrDepartment.DataTextField = "Deptname";
            ddrDepartment.DataValueField = "Deptid";
            ddrDepartment.DataBind();
            ddrDepartment.Items.Insert(0, new ListItem("—选择所属部门—", "0"));
            Dep.Close();
        }
        else
        {
            Response.Write("<script>alert('部门为空请添加部门！');</script>");
          
            ddrDepartment.Items.Insert(0, new ListItem("—选择所属部门—", "0"));
        }
        ZSql type = new ZSql();
        type.Open("select * from CS_WorkTitle");
        if (type.m_table.Rows.Count > 0)
        {
            type.Open("select * from CS_WorkTitle where AuditState=1");
            if (type.m_table.Rows.Count > 0)
            {
                ddltype.DataSource = type.m_table;
                ddltype.DataTextField = "WTitle";
                ddltype.DataValueField = "WTId";
                ddltype.DataBind();
                ddltype.Items.Insert(0, new ListItem("—选择所属职务—", "0"));
                type.Close();
            }
            else
            {
                Response.Write("<script>alert('职务未审核！');</script>");
                
                ddltype.Items.Insert(0, new ListItem("—选择所属职务—", "0"));
            }
        }
        else
        {
            Response.Write("<script>alert('职务为空请添加职务！');</script>");
            ddltype.Items.Insert(0, new ListItem("—选择所属职务—", "0"));
        }
      
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            WageDate.Text = DateTime.Now.ToShortDateString();
            hidID1.Value = Request.QueryString["moduid"].ToString();
            lblTime.Text = DateTime.Now.ToShortDateString();
            lblUser.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            lblwz.Text = f.getModuleidAdd(Request["moduid"]);
            StaffPercentage.Visible = false;
            
                   
            if (Request.QueryString["action"].ToString() == "update")
            {
                hidId.Value = Request.QueryString["StaffId"].ToString();
                WageDate.Attributes.Add("onclick", "SelectDate('WageDate')");
                btnSave.Text = " 修改 ";
                ZSql zs = new ZSql();
                zs.Open("select * from CS_Staff where StaffId=" + hidId.Value.Trim());
                ZSql dep = new ZSql();
                dep.Open("select * from Department ");
                ddrDepartment.DataSource = dep.m_table;
                ddrDepartment.DataTextField = "Deptname";
                ddrDepartment.DataValueField = "Deptid";
                ddrDepartment.DataBind();
                dep.Close();
                ddrDepartment.Items.Insert(0, new ListItem("—选择所属部门—","0"));
                ddrDepartment.SelectedValue = zs.m_table.Rows[0]["Deptid"].ToString();
                ZSql zw = new ZSql();
                zw.Open("select * from CS_WorkTitle where AuditState=1 ");
                ddltype.DataSource = zw.m_table;
                ddltype.DataTextField = "WTitle";
                ddltype.DataValueField = "WTId";
                ddltype.DataBind();
                zw.Close();
                ddltype.Items.Insert(0, new ListItem("—选择所任职务—","0"));
                ddltype.SelectedValue = zs.m_table.Rows[0]["StaffWork"].ToString();

            }
            if (Request.QueryString["action"].ToString() == "add")
            {

                WageDate.Attributes.Add("onclick", "SelectDate('WageDate')");
                initType1();
                btnSave.Text = " 添加 ";
            }
            if (Request.QueryString["action"].ToString() == "flag")
            {
                hidId.Value = Request.QueryString["StaffId"].ToString();
        
                btnSave.Text = " 审核 ";
                ZSql zs = new ZSql();
                zs.Open("select * from CS_Staff where StaffId=" + hidId.Value.Trim());
                ZSql dep = new ZSql();
                dep.Open("select * from Department ");
                ddrDepartment.DataSource = dep.m_table;
                ddrDepartment.DataTextField = "Deptname";
                ddrDepartment.DataValueField = "Deptid";
                ddrDepartment.DataBind();
                dep.Close();
                ddrDepartment.Items.Insert(0, new ListItem("—选择所属部门—","0"));
                ddrDepartment.SelectedValue = zs.m_table.Rows[0]["Deptid"].ToString();
                ZSql zw = new ZSql();
                zw.Open("select * from CS_WorkTitle where AuditState=1 ");
                ddltype.DataSource = zw.m_table;
                ddltype.DataTextField = "WTitle";
                ddltype.DataValueField = "WTId";
                ddltype.DataBind();
                zw.Close();
                ddltype.Items.Insert(0, new ListItem("—选择所任职务—","0"));
                ddltype.SelectedValue = zs.m_table.Rows[0]["StaffWork"].ToString();
                if (zs.m_table.Rows[0]["AuditState"].ToString() == "1")
                {
                    rbtnFlag.Checked = true;
                   rbtnnotFlag.Checked = false;
                }
                if (zs.m_table.Rows[0]["AuditState"].ToString() == "0")
                {
                    rbtnFlag.Checked = false;
                    rbtnnotFlag.Checked = true;
                }
            }
            
          
            initType();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            int flag;
            if (rbtnFlag.Checked)
                flag = 1;
            else
                flag = 0;

            ZSql zs = new ZSql();
            if (!string.IsNullOrEmpty(hidId.Value))
            {
                zs.Open("update CS_Staff set StaffName='" + Common.FormatParameter(StaffName.Text) + "',StaffWork=" + ddltype.SelectedValue + ",Deptid=" + ddrDepartment.SelectedValue + ",StaffTel='" + Common.FormatParameter(StaffTel.Text) + "',StaffAddres='" + Common.FormatParameter(StaffAddres.Text) + "',StaffSalary=" + Convert.ToDouble(StaffSalary.Text) + ",StaffPercentage=" + Convert.ToDouble(StaffPercentage.Text) + ",StaffPrice=" + Convert.ToDouble(StaffPrice.Text) + ",WageDate='" + WageDate.Text + "',StaffOrder=" + Convert.ToInt32(StaffOrder.Text) + ",StaffDiscripte='" + Common.FormatParameter(StaffDiscripte.Text) + "',EditUserName='" + lblUser.Text + "',EditDate='" + lblTime.Text + "',AuditState=" + flag + " where StaffId=" + hidId.Value);
                Response.Write("<script>window.location='Staff.aspx?moduid=" + Request["moduid"] + "';</script>");
              

            }
            else
            {
                if (ddltype.SelectedValue == "0")
                {
                    Response.Write("<script>alert('请选择职务');location.href=document.referrer</script>");
                    return;
                }
                if (ddrDepartment.SelectedValue == "0")
                {
                    Response.Write("<script>alert('请选择部门');location.href=document.referrer</script>");
                    return;
                }
                //ZSql z = new ZSql();
                //z.Open("select * from CS_Staff where StaffName='" +Common.FormatParameter(StaffName.Text) + "'");
                //if (z.m_table.Rows.Count > 0)
                //{
                //    Response.Write("<script>alert('该员工已存在');location.href=document.referrer</script>");
                    
                //    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                //    return;
                //}
                //z.Close();
                zs.Open("insert into CS_Staff (StaffName,StaffWork,Deptid,StaffTel,StaffAddres,StaffPercentage,StaffPrice,StaffSalary,WageDate,StaffOrder,StaffDiscripte,EditUserName,EditDate,workSate,AuditState) values('" + Common.FormatParameter(StaffName.Text) + "'," + ddltype.SelectedValue + "," + ddrDepartment.SelectedValue + ",'" + Common.FormatParameter(StaffTel.Text) + "','" + Common.FormatParameter(StaffAddres.Text) + "'," + Convert.ToDouble(StaffPercentage.Text) + "," + Convert.ToDouble(StaffPrice.Text) + "," + Convert.ToDouble(StaffSalary.Text) + ",'" + WageDate.Text + "','" + Common.FormatParameter(StaffOrder.Text) + "','" + Common.FormatParameter(StaffDiscripte.Text) + "','" + lblUser.Text + "','" + lblTime.Text + "'," + ddlsaletype.SelectedValue + "," + flag + ")");
                Response.Write("<script>window.location='Staff.aspx?moduid=" + Request["moduid"] + "';</script>");
            }
            zs.Close();
        }
        catch (Exception)
        {
            if (Request.QueryString["action"].ToString() == "add")
            {
                Response.Write("<script>alert('添加失败');location.href=document.referrer</script>");
            }
            if (Request.QueryString["action"].ToString() == "flag")
            {
                Response.Write("<script>alert('审核失败');location.href=document.referrer</script>");
            }
            if (Request.QueryString["action"].ToString() == "update")
            {
                Response.Write("<script>alert('修改失败');location.href=document.referrer</script>");
            }
           
        }
    }

    private void initType()
    {
        ZSql type = new ZSql();
        if (!string.IsNullOrEmpty(hidId.Value))
        {
            StaffPrice.Attributes["contentEditable"] = "false";
            type.Open("select StaffName,StaffTel,StaffAddres,StaffPercentage,StaffPrice,StaffSalary,convert(char(10),WageDate,120) as WageDate,StaffOrder,StaffDiscripte from CS_Staff where StaffId=" + hidId.Value);
            StaffName.Text = type.m_table.Rows[0]["StaffName"].ToString();
            StaffTel.Text = type.m_table.Rows[0]["StaffTel"].ToString();
            StaffAddres.Text = type.m_table.Rows[0]["StaffAddres"].ToString();
            StaffPercentage.Text = type.m_table.Rows[0]["StaffPercentage"].ToString();
            StaffPrice.Text = type.m_table.Rows[0]["StaffPrice"].ToString().Substring(0, type.m_table.Rows[0]["StaffPrice"].ToString().Length-2);
            StaffSalary.Text = type.m_table.Rows[0]["StaffSalary"].ToString().Substring(0, type.m_table.Rows[0]["StaffSalary"].ToString().Length-2);
            WageDate.Text = type.m_table.Rows[0]["WageDate"].ToString();
            StaffOrder.Text = type.m_table.Rows[0]["StaffOrder"].ToString();
            StaffDiscripte.Text = type.m_table.Rows[0]["StaffDiscripte"].ToString();
            type.Close();
        }
    }
   
  
}
